﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace mirror
{
    class cVFXLightning : cVFXBase
    {
        Random _random = new Random();
        int mNumForks;
        Vector3 mStart;
        Vector3 mEnd;

        List<Vector3> mPath;

        public cVFXLightning(Vector3 zStart, Vector3 zEnd)
            : this( zStart, zEnd, 0)
        {
        }

        public cVFXLightning(Vector3 zStart, Vector3 zEnd, int zNumForks)
        {
            mPath = new List<Vector3>();

            GeneratePath(zStart, zEnd, zNumForks, 0.1f);
        }


        public cVFXLightning(Vector3 zStart, Vector3 zEnd, int zNumForks, float zWriggleAmplitude)
        {
            mPath = new List<Vector3>();

            GeneratePath(zStart, zEnd, zNumForks, zWriggleAmplitude);
        }

        public void GeneratePath(Vector3 zStart, Vector3 zEnd, int zNumForks, float zWriggleAmplitude)
        {
            mPath.Clear();

            mStart = zStart;
            mEnd = zEnd;
            mNumForks = zNumForks;

            // generate our path right away:

            // find the line which leads straight from start to end
            Vector3 straight = mEnd - mStart;
            Vector3 up = new Vector3(0, 1, 0);
            Vector3 perp = Vector3.Cross(straight, up);

            // num sections on lightning bolt is down to how long the bolt is:
            float wriggleAmplitude = zWriggleAmplitude;
            float sectionLength = 0.4f;
            int numSections = (int)(straight.Length() / sectionLength); // each section will be 0.1f long...
            
            Vector3 sectionIncrememt = straight / numSections;
            // we'll add a fork by just making a new lightning effect, when we've figure out where to put it
            for (int i = 0; i < numSections; i++ )
            {
                mPath.Add(mStart + (sectionIncrememt * i));
            }

            for (int i = 1; i < numSections; i++)
            {
                // we get a random int from 0 to 100, we then scale that back into 0 to 1.0f
                float howmuchperp = (float)_random.Next(100);
                howmuchperp /= 100.0f;
                // then we scale THAT 0 to 1.0f value by our wriggleAmplitude (ie the MAX value)
                howmuchperp *= wriggleAmplitude;

                mPath[i] += howmuchperp * perp;
            }

            for(int i = 0 ;i < mNumForks; i++)
            {
                // generate a fork at the right point:
                Vector3 start = mPath[(numSections / zNumForks) * (i + 1) - 1];
                float perpAmount = (float)_random.Next(100) / 100.0f ;

                Vector3 end = start + perp * perpAmount;
                cVFXManager.Instance.AddEffect(new cVFXLightning(start, end, 0));
            }
        }

        //  Return true if the effect is still active, otherwise it will be removed.
        public override bool Update(GameTime zGameTime)
        {
            base.Update(zGameTime);

            const float effectDuration = 0.25f;

            if (mTimeActive > effectDuration)
            {
                return false;
            }
            return true;
        }

        public override void Draw()
        {
            base.Draw();

            //for (int i = 0; i < mPath.Count - 1; i++)
            //   Ugg.Ugg.Instance.AddLine3D(mPath[i], mPath[i + 1], Color.LightBlue);
        }
    }
}
